<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Ccomments extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = C\lang::inst('dd');
		$view->assign('status', $dlang->get('sh0007'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/comment.ext.js');
	}
	
	/**
	 * @desc    删除评论配置
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids    = \classlib\validator\auditor::intstr2arr('ids');
		$orm    = C\model::load('pro.pl.comment');
		$fields = array('status'=>0, 'stime'=>C\config::$nowtime);
		foreach ($ids as $id) {
			$orm->save($id, $fields);
			$orm->multi_save(array('pid'=>$id), $fields);
		}
		Cadmin::log2db(implode(',', $ids));
		$view->display();
	}
	
	/**
	 * 获取评论资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array('reftype'=>1, 'pid'=>0);
		$refid  = input::get_post('refid');
		if (is_numeric($refid)) {
			$where[] = array('refid', $refid, \classlib\orm::OP_EQ);
		}
		$sdate  = input::get_post('sdate');
		if ($sdate) {
			$where[] = array('addtime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::get_post('edate');
		if ($edate) {
			$where[] = array('addtime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$orm    = C\model::load('pro.pl.comment');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['addtime']  = date('Y-m-d H:i:s', $item['addtime']);
				$item['zmsg']     = '';
				$item['zstatus']  = '';
				$item['zaddtime'] = '';
				$item['msg']      = htmlspecialchars($item['msg']);
				$reply = $orm->get_item(array('pid'=>$item['id']));
				if ($reply) {
					$item['zmsg']     = htmlspecialchars($reply['msg']);
					$item['zstatus']  = $reply['status'];
					$item['zaddtime'] = date('Y-m-d H:i:s', $reply['addtime']);
				}
				$item['anchor']   = Cadmin::user($item['refid']);
				$item['username'] = Cadmin::user($item['userid']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}